package com.hs.hbp.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hs.core.utils.PageParam;
import com.hs.core.utils.PageUtil;
import com.hs.hbp.model.Basicinfo;
import com.hs.hbp.model.Bookinfo;
import com.hs.hbp.model.Doctorinfo;
import com.hs.hbp.model.Hospitalinfo;
import com.hs.hbp.model.Medicineinfo;
import com.hs.hbp.service.IHbpZhuService;
@Controller  
public class SearchController extends AbstractAction {
	private static final Logger logger = Logger.getLogger(SearchController.class);
	private IHbpZhuService hbpZhuService;
	 @Autowired   
	 public void setHbpZhuService(IHbpZhuService hbpZhuService) {
		this.hbpZhuService = hbpZhuService;
	}
	/**
	  * 古籍检索
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/listOldbook.do")   
	public ModelAndView listOldbook(HttpServletRequest request,
			HttpServletResponse response,ModelMap modelMap) throws Exception { 
		PageParam pageParam = new  PageParam(request,"listOldbook.do");
		PageUtil pageUtil = null;
		StringBuffer searchBuffer = new StringBuffer(" select a from Bookinfo a where 1=1  ");
		 
		String field = request.getParameter("field");  
		String q = request.getParameter("searchStr");  
	    String onesql = request.getParameter("onesql");
	    logger.debug("onesql:" + onesql);

		request.setAttribute("field", field);
		request.setAttribute("searchStr", q); 
		if(StringUtils.isNotBlank(field)){
			pageParam.getConditionMap().put("field", field);
			if(StringUtils.isNotBlank(q)){
				pageParam.getConditionMap().put("searchStr", q);
				if("title".equals(field)){
					searchBuffer.append(" and a.title like '%"+q+"%' "); 
				}
				if("intro".equals(field)){
					searchBuffer.append(" and a.abstract_ like '%").append(q).append("%' "); 
				}
				 
			}
		}  
		if(StringUtils.isNotEmpty(onesql)){
			searchBuffer.append(" and a.id in(").append(onesql).append(")");
		}
		logger.debug("sql:" + searchBuffer.toString());
		//searchBuffer.append(" order by substr(a.publishyear,0,4) desc");
		 
		pageParam.setSearchSql(searchBuffer.toString());
		pageUtil = hbpZhuService.getList(pageParam); 
		modelMap.put("dataList", pageUtil);     
		return new ModelAndView("app/oldbooklist");   
	}
	/**
	  * 医生信息检索
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/listDoctor.do")   
	public ModelAndView listDoctor(HttpServletRequest request,
			HttpServletResponse response,ModelMap modelMap) throws Exception { 
		PageParam pageParam = new  PageParam(request,"listDoctor.do");
		PageUtil pageUtil = null;
		StringBuffer searchBuffer = new StringBuffer(" select a from Doctorinfo a where 1=1  ");
		 
		String field = request.getParameter("field");  
		String q = request.getParameter("searchStr");  
	    String onesql = request.getParameter("onesql");
	    logger.debug("onesql:" + onesql);

		request.setAttribute("field", field);
		request.setAttribute("searchStr", q); 
		if(StringUtils.isNotBlank(field)){
			pageParam.getConditionMap().put("field", field);
			if(StringUtils.isNotBlank(q)){
				pageParam.getConditionMap().put("searchStr", q);
				if("dname".equals(field)){
					searchBuffer.append(" and a.dname like '%"+q+"%' "); 
				}
				if("intro".equals(field)){
					searchBuffer.append(" and a.introinfo like '%").append(q).append("%' "); 
				}
				 
			}
		}  
		if(StringUtils.isNotEmpty(onesql)){
			searchBuffer.append(" and a.id in(").append(onesql).append(")");
		}
		logger.debug("sql:" + searchBuffer.toString());
		//searchBuffer.append(" order by substr(a.publishyear,0,4) desc");
		 
		pageParam.setSearchSql(searchBuffer.toString());
		pageUtil = hbpZhuService.getList(pageParam); 
		modelMap.put("dataList", pageUtil);     
		return new ModelAndView("app/doctorlist");  
	}
	/**
	  * 基础知识信息检索
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/listBasic.do")   
	public ModelAndView listBasic(HttpServletRequest request,
			HttpServletResponse response,ModelMap modelMap) throws Exception { 
		PageParam pageParam = new  PageParam(request,"listBasic.do");
		PageUtil pageUtil = null;
		StringBuffer searchBuffer = new StringBuffer(" select a from Basicinfo a where 1=1  ");
		 
		String field = request.getParameter("field");  
		String q = request.getParameter("searchStr");  
	    String onesql = request.getParameter("onesql");
	    logger.debug("onesql:" + onesql);

		request.setAttribute("field", field);
		request.setAttribute("searchStr", q); 
		if(StringUtils.isNotBlank(field)){
			pageParam.getConditionMap().put("field", field);
			if(StringUtils.isNotBlank(q)){
				pageParam.getConditionMap().put("searchStr", q);
				if("title".equals(field)){
					searchBuffer.append(" and a.title like '%"+q+"%' "); 
				}
				if("intro".equals(field)){
					searchBuffer.append(" and a.introinfo like '%").append(q).append("%' "); 
				}
				 
			}
		}  
		if(StringUtils.isNotEmpty(onesql)){
			searchBuffer.append(" and a.id in(").append(onesql).append(")");
		}
		logger.debug("sql:" + searchBuffer.toString());
		//searchBuffer.append(" order by substr(a.publishyear,0,4) desc");
		 
		pageParam.setSearchSql(searchBuffer.toString());
		pageUtil = hbpZhuService.getList(pageParam); 
		modelMap.put("dataList", pageUtil);     
		return new ModelAndView("app/basiclist");  
	}
	/**
	  * 医院信息检索
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/listHospital.do")   
	public ModelAndView listHospital(HttpServletRequest request,
			HttpServletResponse response,ModelMap modelMap) throws Exception { 
		PageParam pageParam = new  PageParam(request,"listHospital.do");
		PageUtil pageUtil = null;
		StringBuffer searchBuffer = new StringBuffer(" select a from Hospitalinfo a where 1=1  ");
		 
		String field = request.getParameter("field");  
		String q = request.getParameter("searchStr");  
	    String onesql = request.getParameter("onesql");
	    logger.debug("onesql:" + onesql);

		request.setAttribute("field", field);
		request.setAttribute("searchStr", q); 
		if(StringUtils.isNotBlank(field)){
			pageParam.getConditionMap().put("field", field);
			if(StringUtils.isNotBlank(q)){
				pageParam.getConditionMap().put("searchStr", q);
				if("title".equals(field)){
					searchBuffer.append(" and a.title like '%"+q+"%' "); 
				}
				if("intro".equals(field)){
					searchBuffer.append(" and a.introinfo like '%").append(q).append("%' "); 
				}
				 
			}
		}  
		if(StringUtils.isNotEmpty(onesql)){
			searchBuffer.append(" and a.id in(").append(onesql).append(")");
		}
		logger.debug("sql:" + searchBuffer.toString());
		//searchBuffer.append(" order by substr(a.publishyear,0,4) desc");
		 
		pageParam.setSearchSql(searchBuffer.toString());
		pageUtil = hbpZhuService.getList(pageParam); 
		modelMap.put("dataList", pageUtil);     
		return new ModelAndView("app/hospitallist");  
	}
	/**
	  * 药品信息检索
	  * @param request
	  * @param response
	  * @return
	 * @throws Exception 
	  */
	@RequestMapping("/listMedicine.do")   
	public ModelAndView listMedicine(HttpServletRequest request,
			HttpServletResponse response,ModelMap modelMap) throws Exception { 
		PageParam pageParam = new  PageParam(request,"listMedicine.do");
		PageUtil pageUtil = null;
		StringBuffer searchBuffer = new StringBuffer(" select a from Medicineinfo a where 1=1  ");
		 
		String field = request.getParameter("field");  
		String q = request.getParameter("searchStr");  
	    String onesql = request.getParameter("onesql");
	    logger.debug("onesql:" + onesql);

		request.setAttribute("field", field);
		request.setAttribute("searchStr", q); 
		if(StringUtils.isNotBlank(field)){
			pageParam.getConditionMap().put("field", field);
			if(StringUtils.isNotBlank(q)){
				pageParam.getConditionMap().put("searchStr", q);
				if("title".equals(field)){
					searchBuffer.append(" and a.title like '%"+q+"%' "); 
				}
				if("intro".equals(field)){
					searchBuffer.append(" and a.introinfo like '%").append(q).append("%' "); 
				}
				 
			}
		}  
		if(StringUtils.isNotEmpty(onesql)){
			searchBuffer.append(" and a.id in(").append(onesql).append(")");
		}
		logger.debug("sql:" + searchBuffer.toString());
		//searchBuffer.append(" order by substr(a.publishyear,0,4) desc");
		 
		pageParam.setSearchSql(searchBuffer.toString());
		pageUtil = hbpZhuService.getList(pageParam); 
		modelMap.put("dataList", pageUtil);     
		return new ModelAndView("app/medichinelist");  
	}
	/**
	  * 方剂食谱
	  * @param request
	  * @param response
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	@RequestMapping("/fjspDetail.do")   
	public ModelAndView fjspDetail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException { 
		 
		String rid = request.getParameter("rid");
 		Bookinfo obj = null;
		if(StringUtils.isNotBlank(rid)){
			obj = (Bookinfo)hbpZhuService.findByID(Bookinfo.class, rid);
		}
		request.setAttribute("obj", obj);
		 
		return new ModelAndView("app/fjspDetail");  
	}
	/**
	  * 医生
	  * @param request
	  * @param response
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	@RequestMapping("/doctorDetail.do")   
	public ModelAndView doctorDetail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException { 
	 
		String rid = request.getParameter("rid");
 		Doctorinfo obj = null;
		if(StringUtils.isNotBlank(rid)){
			obj = (Doctorinfo)hbpZhuService.findByID(Doctorinfo.class, new Long(rid));
		}
		request.setAttribute("obj", obj);
		 
		return new ModelAndView("app/doctorDetail");  
	}
	/**
	  * 药品检索
	  * @param request
	  * @param response
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	@RequestMapping("/medDetail.do")   
	public ModelAndView medDetail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException { 
		 
		String rid = request.getParameter("rid");
	 
		Medicineinfo obj = null;
		if(StringUtils.isNotBlank(rid)){
			obj = (Medicineinfo)hbpZhuService.findByID(Medicineinfo.class, rid);
		}
		request.setAttribute("obj", obj);
		 
		return new ModelAndView("app/medDetail");  
	}
	/**
	  * 基本检索
	  * @param request
	  * @param response
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	@RequestMapping("/basicDetail.do")   
	public ModelAndView basicDetail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException { 
	 
		String rid = request.getParameter("rid");
		Basicinfo obj = null;
		if(StringUtils.isNotBlank(rid)){
			obj = (Basicinfo)hbpZhuService.findByID(Basicinfo.class, rid);
		}
		request.setAttribute("obj", obj);
		 
		return new ModelAndView("app/basicDetail");  
	}

	/**
	  * 医院检索
	  * @param request
	  * @param response
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	@RequestMapping("/hospitalDetail.do")   
	public ModelAndView hospitalDetail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException { 
	 
		String rid = request.getParameter("rid");
 		Hospitalinfo obj = null;
		if(StringUtils.isNotBlank(rid)){
			obj = (Hospitalinfo)hbpZhuService.findByID(Hospitalinfo.class, rid);
		}
		request.setAttribute("obj", obj);
		 
		return new ModelAndView("app/hospitalDetail");  
	}

 
}
